package com.cloud.message.job;

import com.cloud.common.support.RemoteLock;
import com.cloud.message.service.MessageDelayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MessageDelayJob {

    private static final String LOCK_KEY = "Message:DelayLock";

    @Autowired
    private MessageDelayService messageDelayService;

    @Scheduled(fixedDelay = 1000)
    public void handle() {
        if (!RemoteLock.lock(LOCK_KEY, 0)) {
            return;
        }
        try {
            messageDelayService.handle();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            RemoteLock.unlock(LOCK_KEY);
        }
    }

}
